package com.yuerlive.rainbow.module.recharge.presenter;

import com.google.gson.annotations.SerializedName;
import com.yuerlive.rainbow.api.RechargeApi;
import com.yuerlive.rainbow.api.RechargeService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.MyGoldData;
import com.yuerlive.rainbow.entity.OrderInfo;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.UserProxy;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoyi on 2017/5/25.
 */

public class RechargePresenter extends BasePresenterImpl implements RechargeContract.Presenter {
    private RechargeContract.View view;
    private RechargeService service;

    public RechargePresenter(RechargeContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        service = RechargeApi.getInstance().rechargeservice;
    }

    @Override
    public void getMyGold() {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        User user = UserProxy.getUser();
        service.getMyGold(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<MyGoldData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<MyGoldData> myGoldDataBaseData) {
                        view.showRechargeItem(myGoldDataBaseData.getObject());
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void orderPay(String id, int type) {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        User user = UserProxy.getUser();
        params.put("productId", id);
        params.put("type", String.valueOf(type));
        service.orderPay(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<OrderInfo>>(this) {
                    @Override
                    protected void onSuccess(BaseData<OrderInfo> orderInfoBaseData) {
                        view.hideLoading();
                        view.showOrderInfo(orderInfoBaseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void payNotify(String outTradeNum, String tradeNo) {
        Map<String, String> params = new HashMap<>();
        params.put("outTradeNo", outTradeNum);
        params.put("tradeNo", tradeNo);
        service.payNotify(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData orderInfoBaseData) {
                        L.e("TAG", "payNotify " + orderInfoBaseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }


}
